<?php
/* $data = FileCache::get('models_field_'.$modelid);   
print_r($data['fields']);
exit;*/
?>


<script>
$(function(){
	$.validator.addMethod("check_pattern", function(value, element, param) {
		return this.optional(element) || param.test(value); 
	}, "输入数据错误"); 
	
	$.validator.addMethod("check_rangelength", function(value, element, param) { 
		var length = value.length;
		for(var i = 0; i < value.length; i++){
			if(value.charCodeAt(i) > 127){
				length++;
			}
		}
		return this.optional(element) || ( length >= param[0] && length <= param[1] );   
	}, $.validator.format("请确保输入的值在{0}-{1}个字节之间(一个中文字算2个字节)"));
	
	$.validator.addMethod("check_ueditor", function(value, element, param) { 
		var content = param.getContent();
		return content != ''; 
	}, "输入数据错误"); 
	
	
	
	$("#view-form-form").validate({	
	 	//debug:true,
		rules: {
	<?php
	$i = 1;
	$array_field = array('checkbox');
	foreach($this->fields as $k=>$v){
		
		?>
		"Models[<?php echo $v['field']?>]<?php if(in_array($v['formtype'], $array_field)) echo '[]';?>":{
		<?php
		if ($v['required']){
		?>
			required: true,
		<?php
			if ($v['formtype'] == 'ueditor'){
			?>
				check_ueditor: ue_<?php echo $v['field']?>,
			<?php
			}
		}
		if (@$v['setting']['maxlength'] || @$v['setting']['maxlength']){
		?>
			check_rangelength: [<?php echo intval(@$v['setting']['minlength']).','.@$v['setting']['maxlength']?>],
		<?php
		}
		
		
		//有正则校验
		if (@$v['setting']['pattern']){
		?>
			check_pattern: <?php echo @$v['setting']['pattern']?>,
		<?php
		}
		?>
	  },
		<?php
	}
	
	?>
	},
		messages: {
	<?php
	foreach($this->fields as $k=>$v){
		?>
		"Models[<?php echo $v['field']?>]<?php if(in_array($v['formtype'], $array_field)) echo '[]';?>":{
			required: "<?php echo @$v['setting']['errortip']?>" || '必填项',
		<?php
	
		if (@$v['setting']['pattern']){
		?>
			check_pattern: "<?php echo @$v['setting']['errortip']?>",
		<?php
		}
		?>
	
		},
		<?php
		$i++;
	}
	
	?>
		},
    /*submitHandler: function(form) { //通过之后回调 
        var param = $(form).serialize();
        $.ajax({
            url: $(form).attr('action'),
            type: "post",
            dataType: "json",
            data: param,
            success: function(data) {
                if (data.code == 200) {
                    location.href = data.backurl;
                } else {
                    var jsonObj = eval('(' + result + ')');
                }
            }
        });
    },*/
    invalidHandler: function(form, validator) { //不通过回调 
        return false;
    }   
	});
});
</script>